/***
 * author: Adam Pociejowski
 */

package main;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import requests.*;
import requests.settings.Settings;

public class DemiGui extends JFrame implements KeyListener {
	
	private static final long serialVersionUID = 1L;
	private JPanel gui;
	private JLabel monitor, demi, temperature;
	private TextField field;
	private Font labelFont = new Font("Times New Roman", 1, 30), 
				 fieldFont = new Font("Times New Roman", 1, 22),
				 temperatureFont = new Font("Arial", 1, 35);
	private String mainPath = "C://Users/Adam/Documents/projects/D.E.M.I/";
	private Settings settings = new Settings(mainPath+"settings.txt", this);
	private final Controller controller = new Controller(this, mainPath, settings);
	private Color monitorColor, temperatureColor, demiColor;
	
	DemiGui(int width, int height) {
		String content = settings.getSettings("DemiGuiMonitorColor");
		monitorColor = getColor(content);
		content = settings.getSettings("DemiGuiTemperatureColor");
		temperatureColor = getColor(content);
		content = settings.getSettings("DemiGuiDemiColor");
		demiColor = getColor(content);
		
	
		setTitle("D.E.M.I.");
		setSize(width, height);
		setLocation(600,300);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false);
		
		/********    Main panel with all content       ********/
		
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints(); 
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		
		layout.columnWidths = new int[] { 70, 254, 70 };
		layout.rowHeights = new int[] { 60, 30, 60 };
		
		gui = new JPanel(layout);
		gui.setBackground(demiColor);
		
		
			demi = new JLabel("D.E.M.I.");
			demi.setBackground(demiColor);
			demi.setHorizontalAlignment(SwingConstants.CENTER);
			demi.setFont(labelFont);
			demi.setForeground(Color.yellow);
			demi.setOpaque(true);	
			c.gridy = 0;
			c.gridx = 1;
			gui.add(demi, c);
			
		
			field = new TextField("");
			field.setFont(fieldFont);
			field.addKeyListener(this);
			c.gridwidth = 3;
			c.gridx = 0;
			c.gridy = 1;
			gui.add(field, c);
			
			monitor = new JLabel("");
			monitor.setBackground(monitorColor);
			monitor.setForeground(Color.black);
			monitor.setOpaque(true);	
			c.gridy = 2;
			gui.add(monitor, c);
			
			temperature = new JLabel("");
			temperature.setBackground(temperatureColor);
			temperature.setForeground(Color.black);
			temperature.setFont(temperatureFont);
			temperature.setHorizontalAlignment(SwingConstants.CENTER);
			temperature.setOpaque(true);	
			c.gridy = 0;
			c.gridx = 2;
			c.gridwidth = 1;
			gui.add(temperature, c);
			
			
		add(gui);
		
		/******************************************************/
		
		
	}
	
	public void exit() {
		System.exit(1);
	}

	
	public void Monitor(String text) {	
		monitor.setText(text);
	}
	
	public void Temperature(String text) {
		temperature.setText(text);
		TemperatureColor(Double.parseDouble(text));
	}
	
	private Color getColor(String content) {
		int tmp[] = new int[3];
		tmp[0] = Integer.parseInt(content.substring(0,3));
		tmp[1] = Integer.parseInt(content.substring(4,7));
		tmp[2] = Integer.parseInt(content.substring(8,11));
		Color color = new Color(tmp[0],tmp[1],tmp[2]);
		return color;
	}
	
	public void TemperatureColor(double value) {
		
		int r,g,b;
		r = (int)value * 6 + 120;
		if (r > 255) 
			r = 255;
		
		if (value > 20)
			g = 220 - (int)value * 30; 
		else
			g = 100 + (int)value * 6;
		
		b = 255 - ((int)value + 10) * 10;
				
		temperatureColor = new Color(r,g,b);	
		temperature.setBackground(temperatureColor);
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) { 
			controller.task(field.getText());
			clear();
		}	
	}
	
	public void clear() {
		field.setText("");
	}

	@Override
	public void keyReleased(KeyEvent arg0) {}

	@Override
	public void keyTyped(KeyEvent arg0) {}
		

}
